// Agent basis in project massimEcomp
/* agent perceives an area that is a square with the size 17 with
   the agent in the center. Each agent is able to see 289 cells. */

/* Initial beliefs and rules */
/* BELIEFS */


/* RULES */



/* initial DESIRES */
!search_cows.


/* INTENTIONS (PLANS AND ACTIONS) */
+!find_pos : true
	<- 	.wait("+pos(_,_,_)").

+!search_cows : not pos(_,_,_)
	<- 	!find_pos;
	   	!search_cows.

+!search_cows : pos(_,_,_) & not cow(_,_,_)
  	<- 	!move;
     	!search_cows.

+!search_cows : pos(_,_,_) & cow(_,_,_)
  	<- 	!goto_cow_position.


+!search_cows : pos(Px,Py,_) & cow(_,Cx,Cy) & fence(Fx,Fy,S)
  	<- 	if(S == closed){
			.print("fence fechado");
			!search_switch;	
		}else{
			.print("fence aberto");
			!goto_cow_position;
		}
		.

+!goto_cow_position : pos(X,Y,_) & cow(_,Cx,Cy)
  	<- 	br.poli.ecomp.sma.massim.action.FindPos(X,Y,Cx,Cy,NewX,NewY);
	    br.poli.ecomp.sma.massim.action.FindDirection(X,Y,NewX,NewY,Direction);
	    moveTo(Direction);
	    !goto_cow_position.


+!move : pos(X,Y,_) & not target(_,_)
  	<- 	.print("move random");
     	br.poli.ecomp.sma.massim.action.RandomPos(X,Y,NewX,NewY);
     	br.poli.ecomp.sma.massim.action.FindDirection(X,Y,NewX,NewY,Direction);
     	moveTo(Direction);
		moveTo(north);
     	.wait("+pos(_,_,_)").

+!move : pos(X,Y,_) & target(Cx,Cy) & Cx \== X & Cy \== Y
	<- 	.print("move to target");
	    br.poli.ecomp.sma.massim.action.FindPos(X,Y,Cx,Cy,NewX,NewY);
	    br.poli.ecomp.sma.massim.action.FindDirection(X,Y,NewX,NewY,Direction);
	    moveTo(Direction);
	    !move.

+!move : pos(X,Y,_) & target(Cx,Cy) & Cx == X & Cy == Y
  	<- 	.print("target end");
     	-target(Cx,Cy);
     	!search_cows.
     	
     	